//
//  MBProgressHUD+category.m
//  FeiHuNote
//
//  Created by N-X on 2017/12/14.
//  Copyright © 2017年 MRNAN. All rights reserved.
//  在MBProgressHUD基础上添加一些自定义功能

#import "MBProgressHUD+category.h"
#import <MBProgressHUD.h>
#import <FLAnimatedImage.h>

@implementation MBProgressHUD (category)


+ (MBProgressHUD *)showInformation:(NSString *)information toView:(UIView *)view andAfterDelay:(float)afterDelay{
    
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;

    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor blackColor];
    
    hud.mode = MBProgressHUDModeText;
    
    hud.label.text = information;
    
    hud.label.numberOfLines = 0;
    
    hud.offset = CGPointMake(0, -20);
    
    [hud hideAnimated:YES afterDelay:afterDelay];
    
    return hud;
}


+ (void)showSuccessWithStatus:(NSString *)status inView:(UIView *)view
{
    UIImageView * successImageView = [[UIImageView alloc]init];
    successImageView.image = [UIImage imageNamed:@"success"];
    [self showCustomView:successImageView status:status inView:view afterDelay:2];
}

+ (void)showErrorWithStatue:(NSString *)status inView:(UIView *)view
{
    UIImageView * errorImageView = [[UIImageView alloc]init];
    errorImageView.image = [UIImage imageNamed:@"error"];
    [self showCustomView:errorImageView status:status inView:view afterDelay:2];
}

+ (void)showLoadingViewWithGif:(NSString *)gif status:(NSString *)status inView:(UIView *)view
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    //自定义imageView
//    UIImageView *cusImageV = [[UIImageView alloc] initWithImage:[self nx_animatedGIFNamed:gif]];
    FLAnimatedImageView *cusImageV = [[FLAnimatedImageView alloc] init];
    cusImageV.animatedImage = [self nx_animatedGIFNamed:gif];
    //设置hud模式
    hud.mode = MBProgressHUDModeCustomView;
    //设置在hud影藏时将其从SuperView上移除,自定义情况下默认为NO
    hud.removeFromSuperViewOnHide = YES;
    //设置方框view为该模式后修改颜色才有效果
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    //设置方框view背景色
    hud.bezelView.backgroundColor = [UIColor clearColor];
    //设置总背景view的背景色，并带有透明效果
    hud.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    hud.customView = cusImageV;
    if (status.length) {
        hud.label.text = status;
    }
}

+ (instancetype)showLoadingViewWithAnimatedImageArray:(NSArray *)animatedImageArray status:(NSString *)status inView:(UIView *)view
{
    if (view == nil) {
        view  = (UIView *)[[UIApplication sharedApplication].delegate window];
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    if (!isFull) {
//        hud.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64);
//    }
    if (animatedImageArray == nil) {
        return hud;
    } else {
        hud.mode = MBProgressHUDModeCustomView;
        UIImageView *imaegCustomView = [[UIImageView alloc] init];
        imaegCustomView.animationImages = animatedImageArray;
        [imaegCustomView setAnimationRepeatCount:0];
        [imaegCustomView setAnimationDuration:(animatedImageArray.count + 1) * 0.075];
        [imaegCustomView startAnimating];
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.color = [UIColor clearColor];
        hud.customView = imaegCustomView;
        hud.square = NO;
        return hud;
    }
}

+ (void)showCustomView:(UIView *)customView status:(NSString *)status inView:(UIView *)view afterDelay:(CGFloat)afterDelay
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.mode = MBProgressHUDModeCustomView;
    
//    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    
//     设置白底黑字
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor blackColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleBlur;
    
    hud.customView = customView;
    
//    hud.square = YES;
    
    hud.label.text = status;
    hud.offset = CGPointMake(0, -20);
    [hud hideAnimated:YES afterDelay:afterDelay];
}

#pragma mark - private method

+ (FLAnimatedImage *)nx_animatedGIFNamed:(NSString *)name {
    CGFloat scale = [UIScreen mainScreen].scale;
    if (scale > 1.0f) {
        NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
        
        NSData *data = [NSData dataWithContentsOfFile:retinaPath];
        
        if (data) {
            return [FLAnimatedImage animatedImageWithGIFData:data];
        }
        
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
        
        data = [NSData dataWithContentsOfFile:path];
        FLAnimatedImage * image = [FLAnimatedImage animatedImageWithGIFData:data];
        if (data) {
            return [FLAnimatedImage animatedImageWithGIFData:data];
        }
        
        return [UIImage imageNamed:name];
    }
    else {
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        if (data) {
            return [FLAnimatedImage animatedImageWithGIFData:data];
        }
        
        return [UIImage imageNamed:name];
    }
}

@end
